<template>
  <div class="login-container">
    <div class="left">
      <img src="@/assets/common/login_back.png">
    </div>
    <div class="right">
      <el-card class="layui-anim layui-anim-scale">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <div class="title-container">
            <h1 class="title">I H R M 后 台 管 理 系 统</h1>
          </div>
          <el-form-item prop="mobile">
            <span class="svg-container"><svg-icon icon-class="user"/></span>
            <el-input ref="username" v-model="loginForm.mobile" placeholder="请输入手机号" type="text"/>
          </el-form-item>
          <el-form-item prop="password">
            <span class="svg-container"><svg-icon icon-class="password"/></span>
            <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType" placeholder="请输入密码" @keyup.enter.native="handleLogin"/>
            <span class="show-pwd" @click="showPwd"><svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/></span>
          </el-form-item>
          <el-form-item prop="isAgree" class="checked">
            <el-checkbox v-model="loginForm.isAgree">用户平台使用协议</el-checkbox>
          </el-form-item>
          <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
/* 引入用于验证手机号和密码的回调函数 */
import { validMobile, validPassword } from '@/utils/validate'

export default {
  name: 'Login',
  data() {
    /*
    *验证手机号的回调函数
    *参数一rule:验证的规则 参数二value:校验的值 参数三callback：放行函数
    */
    const validateMobile = (rule, value, callback) => {
      if (!value.trim().length) return callback(new Error('手机号是必填项'))
      if (!validMobile(value)) return callback(new Error('输入的手机号不合格'))
      callback()
    }
    /*
    *验证密码的回调函数
    *参数一rule:验证的规则 参数二value:校验的值 参数三callback：放行函数
    */
    const validatePassword = (rule, value, callback) => {
      if (!value.trim().length) return callback(new Error('密码是必填项'))
      if (!validPassword(value)) return callback(new Error('输入的密码不合格'))
      callback()
    }
    /*
    *验证是否复选框是否勾选的回调函数
    *参数一rule:验证的规则 参数二value:校验的值 参数三callback：放行函数
    */
    const validateAgree = (rule, value, callback) => {
      if (!value) return callback(new Error('请勾选用户平台使用协议'))
      callback()
    }
    return {
      /* 表单数据对象 */
      loginForm: {
        // 手机号
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        // 密码
        password: process.env.NODE_ENV === 'development' ? 'hm#qd@23!' : '',
        // 是否勾选
        isAgree: process.env.NODE_ENV === 'development'
      },
      /*
      *表单规则对象
      * validator:自定义校验函数、trigger:什么时候触发校验
      */
      loginRules: {
        mobile: [{ validator: validateMobile, trigger: 'blur' }],
        password: [{ validator: validatePassword, trigger: 'blur' }],
        isAgree: [{ validator: validateAgree, trigger: 'change' }]
      },
      /* 登录按钮的加载状态 */
      loading: false,
      /* 密码款的类型 */
      passwordType: 'password',
      /* 用于重定向 */
      redirect: undefined
    }
  },
  watch: {
    /* 监听路由参数对象$route的变化 */
    $route: {
      handler(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    /* 用于切换密码框类型的回调函数 */
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? '' : 'password'
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    /* 点击登录按钮的回调函数 */
    async handleLogin() {
      /*
      *validate()对整个表单进行校验的方法，参数为一个回调函数(该回调函数会在校验结束后被调用，并传入两个参数：是否校验成功和未通过校验的字段)
      *若不传入回调函数，则会返回一个promise
      */
      const valid = await this.$refs.loginForm.validate().catch(e => e)
      // valid为true则代表表单兜底校验通过，false则代表表单兜底校验失败
      if (!valid) return this.$notify({ title: '警告', message: '请完善登录信息', type: 'warning' })
      // 派发一个action去发送登录请求
      try {
        // 开启按钮的loading效果
        this.loading = true
        // 派发一个'user模块下的loginAction'传入参数this.loginForm，发送登录请求(该action返回的是一个Prmoise对象)
        const { message } = await this.$store.dispatch('user/loginAction', this.loginForm)
        // 提示用户登录成功
        this.$notify({ title: '成功', message, type: 'success' })
        // 跳转路由
        this.$router.push(this.redirect || '/')
      } catch (e) {
        // 派发一个action去发送登录请求时请求失败
        this.$notify({ title: '错误', message: e.message, type: 'error' })
      }
      // 关闭按钮的loading效果
      this.loading = false
    }
  }
}
</script>

<style lang="scss">
$bg: #283443;
$light_gray: #fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

.login-container {
  .el-input {
    display: inline-block;
    height: 50px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #68b0fe;
      height: 47px;
      caret-color: #cccccc;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.5);
    background: #f4f5fb;
    border-radius: 10px;

  }
}
</style>

<style lang="scss" scoped>
$bg: #ffffff;
$dark_gray: #889aa4;
$light_gray: #eee;
/* 登录组件的容器 */
.login-container {
  width: 100%;
  height: 100%;
  min-height: 100%;
  background-color: $bg;
  overflow: hidden;
  display: flex;
  /* 左侧 */
  .left {
    width: 56%;

    img {
      width: 100%;
      height: 100vh;
      object-fit: cover;
      border-top-right-radius: 55px;
      border-bottom-right-radius: 55px;
    }
  }

  /* 右侧 */
  .right {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;

    .el-card {
      border-radius: 20px;


      .el-form {
        position: relative;
        width: 600px;
        padding: 35px;
        margin: 0 auto;
        overflow: hidden;

        .svg-container {
          padding: 6px 5px 6px 15px;
          color: $dark_gray;
          vertical-align: middle;
          width: 30px;
          display: inline-block;
        }

        .title-container {
          position: relative;

          .title {
            font-size: 26px;
            color: #283443;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
          }
        }

        .show-pwd {
          position: absolute;
          right: 10px;
          top: 7px;
          font-size: 16px;
          color: $dark_gray;
          cursor: pointer;
          user-select: none;
        }

        .el-button {
          width: 100%;
          height: 55px;
          font-size: 16px;
          font-weight: 700;
        }

        .checked {
          border: unset;
          background: unset;
          border-radius: unset;
          color: unset;
        }
      }
    }

  }
}
</style>
